package com.guli.member.controller;

import java.util.Arrays;
import java.util.Map;

import com.guli.member.vo.MemberLoginVo;
import com.guli.common.vo.MemberRegistVo;
import com.guli.member.vo.SocialUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.guli.member.entity.MemberEntity;
import com.guli.member.service.MemberService;
import com.guli.common.utils.PageUtils;
import com.guli.common.utils.R;


/**
 * 会员
 * @author zzb
 * @email 1626131069@qq.com
 * @date 2023-11-22 23:36:20
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    /**
     * 社交登录
     * @param socialUser
     * @return
     */
    @PostMapping("/oauth2/login")
    public R oauthLogin(@RequestBody SocialUser socialUser) {
        MemberEntity member = memberService.oauthLogin(socialUser);
        if (member != null) {
            return R.ok().put("code", 0).put("data", member);
        } else {
            return R.error();
        }
    }

    /**
     * 普通登录
     * @param memberLoginVo
     * @return
     */
    @PostMapping("/login")
    public R login(@RequestBody MemberLoginVo memberLoginVo) {
        MemberEntity member = memberService.login(memberLoginVo);
        if (member != null) {
            return R.ok().put("data", member).put("code", 0);
        } else {
            return R.error("账号密码错误");
        }
    }

    /**
     * 注册
     * @param memberRegistVo
     * @return
     */
    @PostMapping("/regist")
    public R regist(@RequestBody MemberRegistVo memberRegistVo) {
        memberService.regist(memberRegistVo);
        return R.ok();
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = memberService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        MemberEntity member = memberService.getById(id);
        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody MemberEntity member) {
        memberService.save(member);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody MemberEntity member) {
        memberService.updateById(member);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        memberService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

}
